package com.example.blog.web;

import com.example.blog.pojo.blog;
import com.example.blog.pojo.tag;
import com.example.blog.pojo.type;
import com.example.blog.service.blogService;
import com.example.blog.service.tagService;
import com.example.blog.service.typeService;
import com.example.blog.util.footerUtile;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collections;
import java.util.List;

@Controller
public class indexController {

    @Autowired
    private blogService blogService;
    @Autowired
    private typeService typeService;
    @Autowired
    private tagService tagService;
    @Autowired
    private footerUtile footerUtile;

    /**
     * 获取所有blog，type，tag。并控制输出的数目
     * @param page
     * @param model
     * @return
     */
    @GetMapping({"/","/index"})
    public String index(@RequestParam(value = "page" ,defaultValue = "1")Integer page, Model model){
        PageHelper.startPage(page,6);
        PageInfo<blog> info = new PageInfo<blog>(blogService.listBlog());
        List<blog> blogs = blogService.listBlog();
        List<type> types = typeService.list();
        List<tag> tags = tagService.list();
        Collections.reverse(blogs);         //blog倒序排列
        if (types.size()>6){
            for (int i = types.size(); i > 5; i--) {
                types.remove(i);
            }

        }if (tags.size()>8){
            for (int i = tags.size(); i > 7; i--) {
                tags.remove(i);
            }
        }if (blogs.size()>8){
            for (int i = blogs.size(); i > 7; i--) {
                blogs.remove(i);
            }
        }
        model.addAttribute("pages",info);
        model.addAttribute("types",types);
        model.addAttribute("tags",tags);
        model.addAttribute("blogs",blogs);
        model.addAttribute("footBlog",footerUtile.footBlog());
        return "index";
    }

    /**
     * 如果有传输type，就只返回含有该type的blog
     * 如果没有传或者为空，则输出所有blog
     * @param page
     * @param model
     * @param type
     * @return
     */
    @GetMapping("/types")
    public String types(@RequestParam(value = "page" ,defaultValue = "1")Integer page,Model model,String type){
        if (type != null && !type.equals("")){
            PageHelper.startPage(page,6);
            PageInfo<blog> info = new PageInfo<blog>(blogService.getBlogByType(type));
            model.addAttribute("pages",info);
            model.addAttribute("active",type);
        }else {
            PageHelper.startPage(page,6);
            PageInfo<blog> info = new PageInfo<blog>(blogService.listBlog());
            model.addAttribute("pages",info);
        }
        List<type> types = typeService.list();
        model.addAttribute("types",types);
        model.addAttribute("footBlog",footerUtile.footBlog());
        return "types";
    }

    /**
     * 如果有传tag的id，就输出含有该tag的blog
     * 如果没穿或者为空，就输出所有blog
     * @param page
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/tags")
    public String tags(@RequestParam(value = "page" ,defaultValue = "1")Integer page,Model model,Integer id){
        if (id != null && !id.equals("")){
            PageHelper.startPage(page,6);
            PageInfo<blog> info = new PageInfo<blog>(blogService.getBlogByTag(id));
            model.addAttribute("pages",info);
            model.addAttribute("active",id);
        }else {
            PageHelper.startPage(page,6);
            PageInfo<blog> info = new PageInfo<blog>(blogService.listBlog());
            model.addAttribute("pages",info);
        }
        List<tag> tags = tagService.list();
        model.addAttribute("tags",tags);
        model.addAttribute("footBlog",footerUtile.footBlog());
        return "tags";
    }

    @GetMapping("/archives")
    public String archives(Model model){
        List<blog> blogs = blogService.listBlog();
        Collections.reverse(blogs);
        model.addAttribute("blogs",blogs);
        model.addAttribute("footBlog",footerUtile.footBlog());
        return "archives";
    }

    @GetMapping("/about")
    public String about(){
        return "about";
    }

    /**
     * 根据id获取blog
     * 每当访问blog就浏览量加一
     * @param id
     * @param model
     * @return
     * @throws NotFoundException
     */
    @GetMapping("/blog")
    public String blog(Integer id,Model model) throws NotFoundException {
        blog blog = blogService.getBlogMarkdown(id);
        model.addAttribute("blog",blog);
        model.addAttribute("footBlog",footerUtile.footBlog());
        return "blog";
    }

    /**
     * 利用传入进来的find字符串查找含有该title的所有blog，并且使用模糊查询。
     * @param page
     * @param find
     * @param model
     * @return
     */
    @GetMapping("/find")
    public String find(@RequestParam(value = "page" ,defaultValue = "1")Integer page,String find,Model model){
        List<tag> tags = tagService.list();
        PageHelper.startPage(page,6);
        PageInfo<blog> info = new PageInfo<blog>(blogService.getBlog("%"+find+"%"));
        model.addAttribute("tags",tags);
        model.addAttribute("pages",info);
        model.addAttribute("str",find);
        model.addAttribute("footBlog",footerUtile.footBlog());
        return "search";
    }
}
